<?php
if (!function_exists('wddj_get_openid')) {
    function wddj_get_openid($wechat_id,$channel = 1){
        if ($channel == 1) {
            $field = 'openid';
        } else {
            $field = 'openid';
        }
        $openid =\app\api\model\wddj\UserWechat::where('id',$wechat_id)->value($field);
        return $openid;
    }
}

if (!function_exists('wddj_full_url')) {
    /**
     * 补全url
     */
    function wddj_full_url($url,$ret=false)
    {
        if (empty($url)) {
            return '';
        }

        if(stripos($url,',') !== false){
            $urls=explode(',',$url);
            foreach ($urls as &$row){
                $row=wddj_full_url($row);
            }
            return $urls;
        }

        if (!wddj_is_url($url)){
            $url = cdnurl($url, true);
            if (!wddj_is_url($url)) {
                $url = request()->domain() . $url;
            }
        }
        return $ret?array($url):$url;
    }
}

if (!function_exists('wddj_is_url')) {
    /**
     * 是否url
     */
    function wddj_is_url($url)
    {
        $preg = "/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is";
        if (preg_match($preg, $url)) {
            return true;
        } else {
            return false;
        }
    }
}

if (!function_exists('wddj_create_order')) {
    /**
     * 创建订单编号
     */
    function wddj_create_order()
    {
        return date('YmdHis') . str_pad(mt_rand(0, 1000), '4', '0', STR_PAD_LEFT);
    }
}

if (!function_exists('remove_wddj_full_url')) {
    /**
     * 移除域名
     */
    function remove_wddj_full_url($url,$ret=false)
    {
        if(stripos($url,',') !== false){
            $urls=explode(',',$url);
            foreach ($urls as &$row){
                $row=remove_wddj_full_url($row);
            }
            return implode(',',$urls);
        }
        $url = remove_wddj_cdnurl($url);
        return $ret?array($url):$url;
    }
}

if (!function_exists('remove_wddj_cdnurl')) {

    /**
     * 移除上传资源的CDN的地址
     * @param string  $url    资源相对地址
     * @return string
     */
    function remove_wddj_cdnurl($url)
    {
        $cdnurl = \think\Config::get('upload.cdnurl');
        if (wddj_is_url($url)) {

            if($cdnurl) {
                $url = str_replace($cdnurl,'',$url);
            } else {
                $url = str_replace(request()->domain(),'',$url);
            }

        }
        return $url;
    }
}

if (!function_exists('wddj_create_order')) {
    /**
     * 创建订单编号
     */
    function wddj_create_order()
    {
        return date('YmdHis') . str_pad(mt_rand(0, 1000), '4', '0', STR_PAD_LEFT);
    }
}

if (!function_exists('wddj_distance')) {
    //计算经纬度距离（php 方法）
    function wddj_distance($lat1, $lon1, $lat2, $lon2) {
        $earthRadius = 6371000; // 地球半径，单位为米

        $lat1 = deg2rad($lat1);
        $lon1 = deg2rad($lon1);
        $lat2 = deg2rad($lat2);
        $lon2 = deg2rad($lon2);

        $deltaLat = $lat2 - $lat1;
        $deltaLon = $lon2 - $lon1;
        $a = sin($deltaLat / 2) * sin($deltaLat / 2) + cos($lat1) * cos($lat2) * sin($deltaLon / 2) * sin($deltaLon / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        $distance = $earthRadius * $c;
        if ($distance < 1000){
            $rounded_number = round($distance, 2);  // 保留两位小数
            $distance_number = $rounded_number.'m';
        }else{
            $kilometers = $distance / 1000;  // 将米转换为千米
            $rounded_number = round($kilometers, 2);  // 保留两位小数
            $distance_number = $rounded_number.'km';
        }
        return $distance_number; // 返回结果保留小数点后两位
    }
}

if (!function_exists('wddj_mkdirs')) {
    //批量创建目录
    function wddj_mkdirs($path, $mode = "0755") {
        if(!is_dir($path)) { // 判断目录是否存在

            wddj_mkdirs(dirname($path), $mode); // 循环建立目录

            mkdir($path, $mode); // 建立目录

        }

        return true;
    }
}

if (!function_exists('wddj_curl_get_https')) {
    function wddj_curl_get_https($url,$header = [],$body_data = [],$url_param = []){ // 模拟提交数据函数
        $jsonData = json_encode($body_data);

        // 初始化CURL
        $curl = curl_init();

        $header = array_merge($header,array(
            'Content-Type:'.'application/json; charset=UTF-8',
        ));
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

// 设置请求URL
        curl_setopt($curl, CURLOPT_URL, $url);

// 设置请求方法为GET
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');

        curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);

// 设置返回响应结果而不是直接输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// 执行请求
        $tmpInfo = curl_exec($curl);

// 获取HTTP状态码
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }

// 关闭CURL会话
        curl_close($curl); // 关闭CURL会话

// 处理响应
        if ($httpCode == 200) {
            // 请求成功
            return $tmpInfo; // 返回数据，json格式
        } else {
            // 请求失败
            echo 'Error: ' . $httpCode;
        }



    }
}

if (!function_exists('wddj_get_waiting_time')) {
    /**
     * Desc 获取时间耗时
     * Create on 2024/11/29 17:28
     * Create by wangyafang
     */
    function wddj_get_waiting_time($end,$start)
    {
        $seconds = bcsub($end,$start);
        if ($seconds < 60) {
            return 0;
        }
        $seconds = bcdiv($seconds,60,2);
        return ceil($seconds);
    }
}





